package com.cbs.android.function.input;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import com.cbs.android.component.application.activity.CBSDialogFragment;

/**
 * Created by zyj on 2016/3/19.
 */
public class DatePickDialogFragment extends CBSDialogFragment {
	private int year = 1970;
	private int month = 0;
	private int day = 1;
	private int layout = R.layout.cbs_input_dialog_datepick;
	private String title;
	private OnConfirmListener onConfirmListener;

	private DatePicker datePicker;

	public DatePickDialogFragment setTitle(String title) {
		this.title = title;
		return this;
	}

	public DatePickDialogFragment setLayout(int layout) {
		this.layout = layout;
		return this;
	}

	public DatePickDialogFragment setBirthday(String birthday) {
		if (birthday == null || birthday.equals("")) {
			return this;
		}
		String[] s = birthday.split("-");
		if (s.length == 3) {
			try {
				int mYear = Integer.parseInt(s[0]);
				int mMonth = Integer.parseInt(s[1]) - 1;
				int mDay = Integer.parseInt(s[2]);
				year = mYear;
				month = mMonth;
				day = mDay;
			} catch (Exception e) {
			}

			return this;
		} else {
			return this;
		}
	}

	public DatePickDialogFragment setOnConfirmListener(OnConfirmListener onConfirmListener) {
		this.onConfirmListener = onConfirmListener;
		return this;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(layout, container, false);

		// TODO need to check all code below
		// TODO layout
		TextView titleView = (TextView) view.findViewById(R.id.cbs_input_dialog_datepick_title);

		datePicker = (DatePicker) view.findViewById(R.id.cbs_input_dialog_datepick_date);
		datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
			@Override
			public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
				year = i;
				month = i1;
				day = i2;
			}
		});

		titleView.setText(title);

		Button cancelView = (Button) view.findViewById(R.id.cbs_input_dialog_datepick_cancel);
		Button submitView = (Button) view.findViewById(R.id.cbs_input_dialog_datepick_submit);
		cancelView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onCancelListener.onClick(DatePickDialogFragment.this);
			}
		});
		submitView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				datePicker.clearFocus();
				int mYear = datePicker.getYear();
				int mMonth = datePicker.getMonth();
				int mDay = datePicker.getDayOfMonth();
				onConfirmListener.onConfirm(DatePickDialogFragment.this, String.format("%04d-%02d-%02d", mYear, mMonth + 1, mDay));
			}
		});
		return view;
	}

	public interface OnConfirmListener {
		void onConfirm(CBSDialogFragment dialogFragment, String birthday);
	}

}
